package com.example.spzx.Service.impl;

import com.alibaba.fastjson.JSON;
import com.example.spzx.Mapper.SysMenuMapper;
import com.example.spzx.Mapper.SysRoleMenuMapper;
import com.example.spzx.Mapper.SysUserRoleMapper;
import com.example.spzx.Service.SysRoleMenuService;
import com.example.spzx.model.dto.system.AssginMenuDto;
import com.example.spzx.model.entity.system.SysMenu;
import com.example.spzx.model.entity.system.SysUser;
import com.example.spzx.model.vo.system.SysMenuVo;
import com.example.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
@Resource
private StringRedisTemplate stringRedisTemplate;
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    public Map<String, Object> getlist(Integer roleId) {
        Map<String, Object> map = new HashMap<>();
    List<SysMenu> list=findNodes();
    map.put("sysMenuList",list);
    List<Long> list1=new ArrayList<>();
    list1=sysRoleMenuMapper.getlist1(roleId);
    map.put("roleMenuIds",list1);
    return map;
    }
    public List<SysMenu> findNodes(){
        List<SysMenu> list=sysMenuMapper.selectAll();
        if(CollectionUtils.isEmpty(list)) return null;

        return convertlinetoTree(list, 0L);


    }
    public List<SysMenu> convertlinetoTree(List<SysMenu> datalist,Long pid){
        List<SysMenu> list=new ArrayList<>();

        for (SysMenu sysMenu:datalist){

            if(sysMenu.getParentId().equals(pid)){

                sysMenu.setChildren(convertlinetoTree(datalist, sysMenu.getId()));
                list.add(sysMenu);
            }
        }
        return list;

    }
    public     void doassign(AssginMenuDto assginMenuDto){
        sysRoleMenuMapper.deletes(assginMenuDto.getRoleId());
        List<Map<String , Number>> list=assginMenuDto.getMenuIdList();
        for (Map<String,Number> map:list){
            System.out.println(map.get("id"));
            sysRoleMenuMapper.insert(assginMenuDto.getRoleId(),map.get("id"));
        }

    }
    public  List<SysMenuVo> getrulelist(String token){
        String UserJSon=stringRedisTemplate.opsForValue().get("user:login" + token);
     SysUser sysUser=   JSON.parseObject(UserJSon, SysUser.class);
        System.out.println(sysUser+"sy");
        Integer userId= Math.toIntExact(sysUser.getId());
       List<SysMenu> list=sysMenuMapper.getlistbyuserId(userId);

        List<SysMenu> treelist= convertlinetoTree(list, 0L);
        List<SysMenuVo> list1=new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu:treelist){
            SysMenuVo sysMenuVo=new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children=sysMenu.getChildren();
            List<SysMenuVo> list2=new ArrayList<>();
            for (SysMenu sysMenu1:children){
                SysMenuVo sysMenuVo1=new SysMenuVo();
                sysMenuVo1.setTitle(sysMenu1.getTitle());
                sysMenuVo1.setName(sysMenu1.getComponent());
                list2.add(sysMenuVo1);
            }
            sysMenuVo.setChildren(list2);
            list1.add(sysMenuVo);

        }
        return list1;



    }
}
